package is.progetto1.aritmeticExpression.builder.impl;

import is.progetto1.aritmeticExpression.builder.InputManagerIF;

public class InputManager implements InputManagerIF {

	private String line;
	private int i = 0;

	public InputManager(String s) {
		this.line = s;
	}

	public String getLine() {
		return line;
	}

	public void setLine(String line) {
		this.line = line;
		i=0;
	}

	@Override
	public String nextSymble() {
		String symble = EOS;
		if (line != null && i < line.length()) {
			while (line.charAt(i) == ' ' && i < line.length())
				i++;// jump space
			if (i < line.length()) {
				if(!(isCharacterOrNumber(line.charAt(i)))){
					char[]c=new char[1];
					c[0]=line.charAt(i);
					symble=new String(c);
					i++;
				}else {
					int j = i;
					while (j < line.length()
							&& isCharacterOrNumber(line.charAt(j))
							) {
						j++;
					}
					symble=line.substring(i,j);
					i++;
				}
			}
		}
		return symble;
	}

	private boolean isCharacterOrNumber(char charAt) {
		if(!(Character.isLetter(charAt))){
			char[]c=new char[1];
			c[0]=charAt;
			if(!(new String(c).matches("[0-9]"))){
				return false;
			}
		}
		return true;
	}

}
